package com.aidex.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.aidex.biz.domain.BizAssessToDeptDetailsFile;
import com.aidex.biz.mapper.BizAssessToDeptDetailsFileMapper;
import com.aidex.common.annotation.DataScope;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.biz.mapper.MonthDeptDetialsMapper;
import com.aidex.biz.domain.MonthDeptDetials;
import com.aidex.biz.service.MonthDeptDetialsService;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;

/**
 * 月报项目详情Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-01-20
 */
@Service
@Transactional(readOnly = true)
public class MonthDeptDetialsServiceImpl extends BaseServiceImpl<MonthDeptDetialsMapper, MonthDeptDetials> implements MonthDeptDetialsService
{
    private static final Logger log = LoggerFactory.getLogger(MonthDeptDetialsServiceImpl.class);

    @Autowired
    private BizAssessToDeptDetailsFileMapper bizAssessToDeptDetailsFileMapper;

    /**
     * 获取单条数据
     * @param monthDeptDetials 月报项目详情
     * @return 月报项目详情
     */
    @Override
    public MonthDeptDetials get(MonthDeptDetials monthDeptDetials)
    {
        MonthDeptDetials dto = super.get(monthDeptDetials);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 月报项目详情id
     * @return 月报项目详情
     */
    @Override
    public MonthDeptDetials get(String id)
    {
        MonthDeptDetials dto = super.get(id);
        return dto;
    }


    /**
     * 查询月报项目详情列表
     * @param monthDeptDetials 月报项目详情
     * @return 月报项目详情
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<MonthDeptDetials> findList(MonthDeptDetials monthDeptDetials)
    {
        return super.findList(monthDeptDetials);
    }

    /**
     * 分页查询月报项目详情列表
     * @param monthDeptDetials 月报项目详情
     * @return 月报项目详情
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<MonthDeptDetials> findPage(MonthDeptDetials monthDeptDetials)
    {
        PageInfo<MonthDeptDetials> pageInfo = super.findPage(monthDeptDetials);
        List<MonthDeptDetials> monthDeptDetialsList= pageInfo.getList();
        if(null!=monthDeptDetialsList&&!monthDeptDetialsList.isEmpty()){
            List<BizAssessToDeptDetailsFile> bizAssessToDeptDetailsFiles = bizAssessToDeptDetailsFileMapper.findListByDetailsIds(monthDeptDetialsList);
            Map<String, List<BizAssessToDeptDetailsFile>> customerDongtaiInfoMap =
                    bizAssessToDeptDetailsFiles.stream().collect(Collectors.groupingBy(BizAssessToDeptDetailsFile::getAssessDeptDetailsId));
            for (MonthDeptDetials monthDeptDetial : monthDeptDetialsList){
                monthDeptDetial.setFileList(customerDongtaiInfoMap.get(monthDeptDetial.getId()));
            }
        }
        return super.findPage(monthDeptDetials);
    }

    /**
     * 保存月报项目详情
     * @param monthDeptDetials
     * @return 结果
     */
    @Override
    public boolean save(MonthDeptDetials monthDeptDetials)
    {
        return super.save(monthDeptDetials);
    }


    /**
     * 删除月报项目详情信息
     * @param monthDeptDetials
     * @return 结果
     */
    @Override
    public boolean remove(MonthDeptDetials monthDeptDetials)
    {
        return super.remove(monthDeptDetials);
    }

    /**
     * 批量删除月报项目详情
     * @param ids 需要删除的月报项目详情ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteMonthDeptDetialsByIds(String[] ids)
    {
        return mapper.deleteMonthDeptDetialsByIds(ids);
    }

    /**
     * 获取最大编号
     * @param monthDeptDetials 月报项目详情
     * @return 结果
     */
    @Override
    public int findMaxSort(MonthDeptDetials monthDeptDetials)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(monthDeptDetials));
    }

    @Override
    @Transactional(readOnly = false)
    public int batchUpdate(List<MonthDeptDetials> monthDeptDetials) {
        try{
            for (MonthDeptDetials monthDeptDetial : monthDeptDetials) {
                super.save(monthDeptDetial);
            }
        }catch (Exception ex){
            log.error("批量保存数据失败",ex);
            return 0;
        }

        return 1;
    }
}
